//
//  TTHttpManager.swift
//  TianTianBiTing
//
//  Created by Gox mac 2 on 2018/12/28.
//  Copyright © 2018 Gox_WallyDiao. All rights reserved.
//

import Foundation
import Alamofire
import SwiftyJSON

class TTHttpManager {
    
    static let sharedInstance = TTHttpManager()
    
    var baseUrl = ""
    private var sessionManager: SessionManager!
    
    init() {
        let serverTrustPolicies: [String: ServerTrustPolicy] = [
            baseUrl: ServerTrustPolicy.pinCertificates(
                certificates: ServerTrustPolicy.certificates(),
                validateCertificateChain: true,
                validateHost: true
            )
        ]
        self.sessionManager =  SessionManager(serverTrustPolicyManager: ServerTrustPolicyManager(policies: serverTrustPolicies))
        let config = URLSessionConfiguration.default
        config.timeoutIntervalForRequest = 60    // 秒
    }
    
    func performRequest(_ url: String, method:Alamofire.HTTPMethod , parameters: Parameters? , headers: HTTPHeaders? ,completionHandler: @escaping (Alamofire.DataResponse<Any>?) -> Void) {
        let encodedStr = url.addingPercentEncoding(withAllowedCharacters: NSCharacterSet.urlQueryAllowed)
        print(url)
        self.sessionManager.request(encodedStr!, method: method, parameters: parameters, encoding: JSONEncoding.default, headers: headers).responseJSON { (response) in
            print("\(url) \n \(String(describing: parameters)) \n \(response)")
            completionHandler(response)
        }
    }
}
